﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public class HComboBox : UltraCombo
    {
        /// <summary></summary>
        private bool _isMultiColumn = false;

        /// <summary></summary>
        private UltraGridColumn _displayColumn;

        /// <summary></summary>
        public string ParentElementType { get; set; }

        /// <summary></summary>
        private ElementBase _parentElement;

        /// <summary></summary>
        private Type _elementType;

        /// <summary></summary>
        public bool AllowNullElement { get; set; }

        /// <summary></summary>
        public bool AllowAddElement { get; set; }

        /// <summary></summary>
        public bool AllowElementLink { get; set; }

        /// <summary>
        /// 
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            this.AfterDropDown += new EventHandler(HComboBox_AfterDropDown);

            this.DisplayStyle = EmbeddableElementDisplayStyle.Office2007;
            base.DisplayLayout.AutoFitStyle = AutoFitStyle.ExtendLastColumn;
            //this.DropDownStyle = UltraComboStyle.DropDownList;
            this.DropDownWidth = this.Width;

            if (!LayoutController.IsInDesignMode(this))
            {
                _elementType = ElementBase.GetType(ParentElementType);

                if (AllowNullElement)
                {
                    EditorButton btnSetNull = new EditorButton("setnull");
                    btnSetNull.Click += new EditorButtonEventHandler(btnSetNull_Click);
                    btnSetNull.ButtonStyle = UIElementButtonStyle.Office2007RibbonButton;
                    btnSetNull.Width = 10;
                    btnSetNull.Appearance.Image = global:: Home.Haushalt.Lib.Properties.Resources.delete_16x16;
                    this.ButtonsRight.Add(btnSetNull);
                }

                if (AllowAddElement)
                {
                    EditorButton btnAddElement = new EditorButton("add");
                    btnAddElement.Click += new EditorButtonEventHandler(btnAdd_Click);
                    btnAddElement.ButtonStyle = UIElementButtonStyle.Office2007RibbonButton;
                    btnAddElement.Width = 10;
                    btnAddElement.Appearance.Image = global:: Home.Haushalt.Lib.Properties.Resources.element_new;
                    this.ButtonsRight.Add(btnAddElement);
                }

                if (AllowElementLink)
                {
                    EditorButton btnLink = new EditorButton("link");
                    btnLink.Click += new EditorButtonEventHandler(btnLink_Click);
                    btnLink.ButtonStyle = UIElementButtonStyle.Office2007RibbonButton;
                    btnLink.Width = 10;
                    btnLink.Appearance.Image = global:: Home.Haushalt.Lib.Properties.Resources.link;
                    this.ButtonsRight.Add(btnLink);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetNull_Click(object sender, EditorButtonEventArgs e)
        {
            this.SelectedRow = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EditorButtonEventArgs e)
        {
            ElementBase elementObj = ElementBase.Instantiate(_elementType);
            elementObj.ParentElement = _parentElement;

            //EditElementProperty(elementObj.New().Row);
            WindowController.EditElementProperty(this, elementObj.New().Row);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLink_Click(object sender, EditorButtonEventArgs e)
        {
            if (this.SelectedRow != null)
            {
                WindowController.EditElementProperty(this, this.SelectedRow.ToDataRow());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentElement"></param>
        /// <param name="dataMember"></param>
        public void AddBinding(ElementBase parentElement, string dataMember)
        {
            _parentElement = parentElement;

            DataSourceBind();

            this.DataBindings.Add("Value", parentElement.BindingTable, dataMember, false, DataSourceUpdateMode.OnPropertyChanged);

            CurrencyManager cm = (CurrencyManager)this.BindingContext[parentElement.BindingTable];
            cm.Position = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        internal void DataSourceBind()
        {
            if (ParentElementType != null) //Für Labels
            {
                this.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True;

                LayoutController.InitColumnLayout(_elementType, DAO.Tables[_elementType], DefaultColumnSets.ForComboBoxControl, this.DisplayLayout);

                this.DataSource = null;
                this.DisplayMember = "";
                this.ValueMember = "";

                //DataSource binden
                this.DataSource = DAO.Tables[_elementType];

                //Kalkulieren
                foreach (UltraGridRow gridRow in this.Rows)
                {
                    ElementBase elementObj = ElementBase.Instantiate(gridRow.ToDataRow());

                    //Kalkulierung der ungebundenden Cells
                    foreach (KeyValuePair<string, object> columnPair in elementObj.CalculateUnboundFields())
                    {
                        if (gridRow.Cells.Exists(columnPair.Key))
                        {
                            gridRow.Cells[columnPair.Key].Value = columnPair.Value;
                        }
                    }

                    //Row Appearances
                    gridRow.CellAppearance.ForeColor = elementObj.GetRowForeColor();
                    gridRow.CellAppearance.BackColor = elementObj.GetRowBackgroundColor();
                }

                SortedList<int, UltraGridColumn> sortedVisibleList = new SortedList<int, UltraGridColumn>();
                foreach (UltraGridColumn column in DisplayLayout.Bands[0].Columns)
                {
                    if (column.Key == "_blank")
                    {
                        column.Hidden = true;
                    }

                    if (!column.Hidden)
                    {
                        sortedVisibleList.Add(column.Header.VisiblePosition, column);
                    }
                }

                if (sortedVisibleList.Count > 0)
                {
                    _displayColumn = sortedVisibleList.Values[0];
                }

                _isMultiColumn = (sortedVisibleList.Count > 1);

                this.DisplayLayout.Bands[0].ColHeadersVisible = _isMultiColumn;

                this.DisplayMember = _displayColumn.Key;
                this.ValueMember = ElementBase.ELEMENTPROPERTY_ID;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HComboBox_AfterDropDown(object sender, EventArgs e)
        {
            if (_displayColumn != null)//für labels
            {
                this.DisplayLayout.Bands[0].SortedColumns.Add(_displayColumn.Key, false);
                this.DropDownWidth = this.Width * 2;

                if (!_isMultiColumn)
                    _displayColumn.Width = this.DropDownWidth - 20;
            }

            if (this.SelectedRow != null)
            {
                this.ActiveRowScrollRegion.ScrollRowIntoView(this.SelectedRow);
                this.ActiveColScrollRegion.ScrollRowIntoView(this.SelectedRow, this.ActiveRowScrollRegion);
            }
        }


        /// <summary>
        /// Für Labels
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="columnName"></param>
        public void DataBindAtColumn(Type elementType, string columnName)
        {
            this.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True;

            this.DataSource = DAO.Tables[elementType].DefaultView.ToTable(true, columnName);
            this.DisplayMember = columnName;
            this.ValueMember = columnName;

            //this.DisplayLayout.Bands[0].Columns[0].Header.Caption = LO.GetColumnName(elementType, columnName);
            //this.DisplayLayout.Bands[0].HeaderVisible = false;
            this.DisplayLayout.Bands[0].ColHeadersVisible = false;
        }
    }
}
